import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EdubaseHolidayAdjustmentSchoolApi } from '#/api/edu/base/setting/holidayAdjustmentSchool';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate, handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { z } from '#/adapter/form';
import { getDeptListCondition } from '#/api/system/dept';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'naturalYear',
      label: '自然年度',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'schoolId',
      label: '学校id',
      component: 'InputNumber',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'adjustType',
      label: '调整类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: [
          { label: '补班（补课）', value: 1 },
          { label: '补休', value: 2 },
        ],
        placeholder: '请选择调整类型',
      },
    },
    {
      fieldName: 'originalDate',
      label: '原日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'adjustDate',
      label: '调整到',
      help: '补班时为上班日期，补休时为休日日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'reason',
      label: '调整原因',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入调整原因',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      component: 'Select',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '调整类型',
      field: 'adjustType',
      render: (val) => {
        return h(DictTag, {
          options: [
            { label: '补班（补课）', value: 1 },
            { label: '补休', value: 2 },
          ],
          value: val,
        });
      },
    },
    {
      label: '自然年度',
      field: 'naturalYear',
    },
    {
      label: '所属假期',
      field: 'holidayName',
    },
    {
      label: '原日期',
      field: 'originalDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '调整到',
      field: 'adjustDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '调整原因',
      field: 'reason',
      render: (val) => {
        return h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        });
      },
    },
    {
      label: '有效状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        });
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'naturalYear',
      label: '自然年度',
      component: 'DatePicker',
      componentProps: {
        allowClear: true,
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'adjustType',
      label: '调整类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: '补班（补课）', value: 1 },
          { label: '补休', value: 2 },
        ],
        placeholder: '请选择调整类型',
      },
    },
    {
      fieldName: 'originalDate',
      label: '原日期',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'adjustDate',
      label: '调整到日期',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'status',
      label: '有效状态',
      component: 'Select',
      defaultValue: CommonStatusEnum.ENABLE,
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择有效状态',
      },
    },
    {
      fieldName: 'areaCode',
      label: '所属行政区划',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptListCondition({ type: 'area' });
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择行政区划',
        treeDefaultExpandAll: true,
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EdubaseHolidayAdjustmentSchoolApi.HolidayAdjustmentSchool>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'naturalYear',
      title: '自然年度',
      minWidth: 120,
    },
    {
      field: 'academicYear',
      title: '学年',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'schoolId',
      title: '学校id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'infraHolidayId',
      title: '全局假期id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'adjustType',
      title: '调整类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            { label: '补班（补课）', value: 1 },
            { label: '补休', value: 2 },
          ],
        },
      },
    },
    {
      field: 'holidayName',
      title: '所属假期名称',
      minWidth: 200,
    },
    {
      field: 'originalDate',
      title: '原日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'adjustDate',
      title: '调整到日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'reason',
      title: '调整原因',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '有效状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'areaCode',
      title: '所属行政区划',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
